package com.cwt_taxi_xa;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.cwt_taxi_xa.constants.Constants;
import com.cwt_taxi_xa.util.StringUtil;
import com.cwt_taxi_xa.util.VersionBiz;

public class ShowVersionInfo extends CWTBaseActivity{

	private static final String TAG = "ShowVersionInfo";
	public static TextView appversion,appVersionName,firmware_version;
	private TextView tv;
	static String version,versionName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.versioninfo);
		handler_refresh = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if(msg.what == 102){
					String firmwareVerisionCode = (String) msg.obj;
					if(!StringUtil.isNull(firmwareVerisionCode)){
						firmware_version.setText(firmwareVerisionCode);
					}
				}else if(msg.what == 103){
					appversion.setText(version);
				}
			}
		};
		appversion = (TextView)this.findViewById(R.id.appversion);
		appVersionName = (TextView)this.findViewById(R.id.app_version_name);
		firmware_version = (TextView)this.findViewById(R.id.firmware_version);
		tv = (TextView) findViewById(R.id.refresh);
		addListener();
		initData();
		appVersionName.setText(versionName);
	}

	private void addListener() {
		// TODO Auto-generated method stub
		tv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				appversion.setText("");
				firmware_version.setText("");
				new Thread(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						try {
							Thread.sleep(200);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						initData();
					}
				}).start();
			}
		});
		findViewById(R.id.btn_app).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				VersionBiz versionBiz = new VersionBiz(ShowVersionInfo.this);
				versionBiz.doCheckVersion(true);
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		int type = getIntent().getIntExtra("type", 0);
		if(type==1){
			VersionBiz versionBiz = new VersionBiz(ShowVersionInfo.this);
			versionBiz.doCheckVersion(true);
		}
	}

	public static Handler handler_refresh;

	/*@Override
	public boolean onTouch(View v, MotionEvent event) {
		super.setCurrentTime(time);
		return false;
	}*/

	public void initData(){
		try {
			/**应用版本号*/
			version = getVersionName(getApplicationContext());
			versionName = getVersionReleashName(getApplicationContext());
			handler_refresh.obtainMessage(103).sendToTarget();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public String getVersionReleashName(Context context){
		PackageManager packageManager=context.getPackageManager();
		PackageInfo packageInfo;
		String versionCode="";
		try {
			packageInfo=packageManager.getPackageInfo(context.getPackageName(),0);
			versionCode=packageInfo.versionName;
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		return versionCode;
	}

	/**
	 * 获取版本名称
	 *
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public String getVersionName(Context context) throws Exception {
//		PackageManager packageManager = context.getPackageManager();
//		PackageInfo packInfo = packageManager.getPackageInfo(
//				context.getPackageName(), 0);
//		return packInfo.versionCode+"";
		return Constants.localVersion+"";
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		handler_refresh = null;
	}

	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub

	}
}
